package mapper

import (
	"cooke-home/auth-service/model/entity"
	"cooke-home/common/handle"
	"errors"

	"gorm.io/gorm"
)

type UserMapperImpl struct {
	DB        *gorm.DB
	TableName string
}

func NewUserMapperImpl() *UserMapperImpl {
	return &UserMapperImpl{
		DB:        handle.DB,
		TableName: "sys_user",
	}
}

func (umi *UserMapperImpl) GetUserByUsernameAndPassword(username string, password string) (*entity.UserEntity, error) {
	var user entity.UserEntity
	err := handle.DB.Table(umi.TableName).
		Where("username = ?", username).
		Where("password = ?", password).
		First(&user).Error

	if errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, errors.New("user not found")
	}
	if err != nil {
		return nil, err // 数据库错误
	}
	return &user, nil
}
